#include "Box.h"
#include "../VectorOperations.h"
#include "../Utilities.h"

Box::Box()
{
	myCenterPos.Set(0,0,0);
	myExtents.Set(0,0,0);
	myMinPos.Set(0,0,0);
	myMaxPos.Set(0,0,0);
}

Box::Box(const Intersection::AABB &anAabb)
{
	myCenterPos = anAabb.myCenterPos;
	myExtents = anAabb.myExtents;
	myMaxPos = anAabb.myMaxPos;
	myMinPos = anAabb.myMinPos;
}
Box &Box::Init(const Vector3<float> &aCenter, const Vector3<float> &someExtents)
{
	myCenterPos = aCenter;
	myExtents = someExtents;
	return Calculate();
}

Box &Box::Init(const Utils::StaticArray<Vector3<float>, 8> &somePoints)
{
	myCenterPos.Set(0,0,0);
	myExtents.Set(0,0,0);
	for (unsigned int index=0; index<somePoints.size(); ++index)
	{
		myCenterPos += somePoints[index];
	}
	myCenterPos /= toFloat(somePoints.size());
	for (unsigned int index=0; index<somePoints.size(); ++index)
	{
		Vector3f side = abs(myCenterPos-somePoints[index]);
		if (side.x>myExtents.x)
		{
			myExtents.x = side.x;
		}
		if (side.y>myExtents.y)
		{
			myExtents.y = side.y;
		}
		if (side.z>myExtents.z)
		{
			myExtents.z = side.z;
		}
	}
	return Calculate();
}

Box &Box::operator=(const Box &aBox)
{
	myCenterPos = aBox.myCenterPos;
	myExtents = aBox.myExtents;
	myMaxPos = aBox.myMaxPos;
	myMinPos = aBox.myMinPos;
	return *this;
}

Box &Box::operator=(const Intersection::AABB &aBox)
{
	myCenterPos = aBox.myCenterPos;
	myExtents = aBox.myExtents;
	myMaxPos = aBox.myMaxPos;
	myMinPos = aBox.myMinPos;
	return *this;
}

Box &Box::Calculate()
{
	myMinPos = myCenterPos - myExtents;
	myMaxPos = myCenterPos + myExtents;
	return *this;
}

Box &Box::operator+=(const Vector3f &aPosition)
{
	myCenterPos += aPosition;
	return Calculate();
}

Box &Box::operator+=(const Vector4f &aPosition)
{
	return operator+=(Vector3f(aPosition.x, aPosition.y, aPosition.z));
}